package com.jk.smartelderlycare.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;

import com.jk.smartelderlycare.dto.SysProcurement;
import com.jk.smartelderlycare.dto.SysStore;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.service.SysStoreServiceFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author xw
 * @version 1.0
 * @ClassName SysStoreController
 * @program: smart-elderly-care-demo
 * @description: TODO
 * @date 2024/12/5 15:14
 */
@Api(tags = "采购入出库")
@Slf4j
@RestController
@RequestMapping(value = "store")
public class SysStoreController {
    @Autowired
    private SysStoreServiceFeign sysStoreServiceFeign;

    //分页查询

    @ApiOperation(value = "分页查询",httpMethod = "POST",notes = "")
    @PostMapping(value = "findSysStorePaginationList")
    public ResponseResult<PagedResponse<SysStore>> findSysStorePaginationList(@RequestBody SysStore sysStore, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        return new ResponseResult<>(200,"查询成功",sysStoreServiceFeign.findSysStorePaginationList(sysStore,pageNum,pageSize));

    }

    @ApiOperation(value = "查询",httpMethod = "GET",notes = "")
    @GetMapping(value = "findStoreList")
    public ResponseResult<List<SysStore>> findStoreList(){
        return new ResponseResult<>(200,"查询成功",sysStoreServiceFeign.findStoreList());
    }
    //新增
    @ApiOperation(value = "新增",httpMethod = "POST",notes = "")
    @PostMapping(value = "addStore")
    public ResponseResult addStore(@RequestBody SysStore sysStore){
        try {
            sysStoreServiceFeign.addStore(sysStore);
            return new ResponseResult(200,"新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444,"新增失败");
        }
    }

    //导出
    @ApiOperation(value = "入库导出", httpMethod = "POST", notes = "信息导出")
    @RequestMapping("/exportExcel")
    public void exportExcel(@RequestBody SysStore sysStore, HttpServletResponse response){
        List<SysStore> storeList = sysStoreServiceFeign.findStoreList();
        //.xlsx格式
        //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //.xls格式
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        //前端存在跨域不成功，设置可访问
        response.setHeader("Access-Control-Allow-Origin","*");
        //设置不要缓存
        response.setHeader("Pragma", "No-cache");
        try {
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("postsInfo", String.valueOf(StandardCharsets.UTF_8));
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
            //设置sheet名
            ExportParams params = new ExportParams();
            params.setSheetName("入库表表");
            //设置标题
            params.setTitle("入库信息");
            // 这里需要设置不关闭流
            Workbook workbook = ExcelExportUtil.exportExcel(params, SysStore.class, storeList);
            //输出流
            OutputStream outStream = response.getOutputStream();
            //浏览器下载
            workbook.write(outStream);
            //关闭流
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }


    @ApiOperation(value = "角色信息导入", httpMethod = "POST", notes = "信息导入")
    @PostMapping(value = "importExcel")
    public ResponseResult importExcel(@RequestPart("file") MultipartFile file) {
        try {
            ImportParams params = new ImportParams();
            params.setHeadRows(2);
            List<SysStore> list = ExcelImportUtil.importExcel(file.getInputStream(), SysStore.class, params);
            list.forEach(store -> {

                sysStoreServiceFeign.addStore(store);
            });
            return new ResponseResult(200, "导入用户信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "导入用户信息失败！");
        }
    }












}
